/**
 * 
 */
package com.thon.service.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.commons.security.utils.Digests;
import com.thon.commons.utils.Encodes;
import com.thon.dao.user.UserDao;
import com.thon.dao.util.Page;
import com.thon.entity.user.User;
import com.thon.service.user.UserService;

/**
 * @author THON
 * @mail thon.ju@meet-future.com
 * @date 2012-2-13 下午09:00:06
 * @description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	@Autowired
	private UserDao userDao;

	@Override
	public void addUser(User user) {
		entryptPassword(user);
		user.setRegistDate(new Date());
		user.setLastLoginDate(new Date());
		user.setPhone(user.getLoginName());
		userDao.save(user);
	}
	
	@Override
	public void registUser(User user) {
		entryptPassword(user);
		user.setRegistDate(new Date());
		user.setLastLoginDate(new Date());
		user.setPhone(user.getLoginName());
		userDao.save(user);
		
	}

	@Override
	public void updateUser(User user) {
		entryptPassword(user);
		userDao.update(user);
	}

	@Override
	public User getUser(String account, String status) {
		User user = userDao.findByLoginName(account, status);
		return user;
	}

	@Override
	public User getUserByEmail(String email) {
		User user = userDao.findByEmail(email);
		return user;
	}
	@Override
	public User getUserByName(String name)
	{
		User user = userDao.findByName(name);
		return user;
	}
	@Override
	public User getUserByName(String name, String status)
	{
		User user = userDao.findByName(name,status);
		return user;
	}
	@Override
	public User getUser(Integer id) {
		User user = userDao.get(id);
		return user;
	}

	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(User user) {
		if(user.getPlainPassword()!=null){
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		user.setSalt(Encodes.encodeHex(salt));

		byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(),
				salt, HASH_INTERATIONS);
		user.setPassword(Encodes.encodeHex(hashPassword));
		}
	}

	@Override
	public String entryptPassword(String password, String salt) {
		byte[] decodeSalt = Encodes.decodeHex(salt);
		byte[] hashPassword = Digests.sha1(password.getBytes(), decodeSalt,
				HASH_INTERATIONS);
		return Encodes.encodeHex(hashPassword);
	}

	@Override
	public List<User> listAllUsers() {
		return userDao.listAllUsers();
	}

	@Override
	public void deleteUser(Integer id) {
		userDao.delete(id);
	}

	@Override
	public void sendMail2User(User user) {

	}

	@Override
	public void resetPassword(User user) {
		entryptPassword(user);
		userDao.save(user);
	}

	@Override
	public Page<User> findUserPage(String role, int pageNo, int pageSize) {
		Page<User> page = new Page<User>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return userDao.readListPage(page);
		//return userDao.findUserPage(page, role);
	}


	
	@Override
	public Page<User> listUserPage(String by, String condition, int pageNo,
			int pageSize) {
		Page<User> page = new Page<User>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		return userDao.listUserPage(page, by, condition);
	}

	@Override

	public User checkOldPassword(int id, String password) {
		User oldUser = userDao.get(id);
		User user = new User();
		user.setPlainPassword(password);
		entryptPassword(user);
		if (user.getPassword().equals(oldUser.getPassword()))
			return oldUser;
		else
			return null;
	}

	@Override
	public void updatePassword(User user) {
		entryptPassword(user);
		userDao.update(user);
	}

	@Override
	public void updateUserLoginInfo(int id) {
		User user = userDao.get(id);
		user.setLastLoginDate(new Date());
		userDao.update(user);
	}

	@Override
	public Page<User> findUserByName(Page<User> page, String name) {
		
		return userDao.findUserByName(page, name);
	}
	
	@Override
	public Boolean nameUnique(Integer userId,String name){
	     List<User> Users = listAllUsers();
	     if(userId==0)
			for (User user : Users) {
				if (name.equals(user.getName()))					
				return false;
			}
	     else
	    	  for(User user:Users)
	 	     {
	 	    	 if(userId==user.getId()&&name.equals(user.getName()))
	 	    	 return false;
	 	     }
	     return true;
	}
	
	
}
